package com.salesys.gr.gui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.events.TraverseListener;
import org.eclipse.swt.events.TraverseEvent;

import com.salesys.gr.util.FrmHelper;
import com.salesys.gr.util.LoginedUserHelper;
import com.salesys.gr.util.MsgBox;
import com.salesys.tony.service.UserService;

import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.MouseAdapter;
import org.eclipse.swt.events.MouseEvent;

public class FrmWithdraw {

	protected Shell FrmWithdraw;
	private Text txtMoney;
	private Text txtPassword;

	/**
	 * Open the window.
	 */
	public void open() {
		Display display = Display.getDefault();
		createContents();
		FrmWithdraw.open();
		FrmWithdraw.layout();
		while (!FrmWithdraw.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
	}

	/**
	 * Create contents of the window.
	 * 
	 * @wbp.parser.entryPoint
	 */
	protected void createContents() {
		FrmWithdraw = new Shell(SWT.CLOSE | SWT.APPLICATION_MODAL);
		FrmWithdraw.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				if (arg0.keyCode == 27) {
					FrmWithdraw.close();
				}
			}
		});
		FrmWithdraw.setSize(210, 150);
		FrmWithdraw.setText("申请提现");
		// 窗口居中
		FrmWithdraw.setLocation(new FrmHelper().setLocationCenter(FrmWithdraw));

		Label lblMoney = new Label(FrmWithdraw, SWT.NONE);
		lblMoney.setBounds(30, 20, 40, 17);
		lblMoney.setText("金额：");

		Label lblPassword = new Label(FrmWithdraw, SWT.NONE);
		lblPassword.setBounds(30, 50, 40, 17);
		lblPassword.setText("密码：");

		txtMoney = new Text(FrmWithdraw, SWT.BORDER);
		txtMoney.setBounds(70, 19, 100, 20);

		txtPassword = new Text(FrmWithdraw, SWT.BORDER);
		txtPassword.setBounds(70, 49, 100, 20);

		Button btnConfirm = new Button(FrmWithdraw, SWT.NONE);
		btnConfirm.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				int affect1 = 0;
				affect1 = new UserService().verifyOldPwd(txtPassword.getText(), LoginedUserHelper.getLoginedUser());
				if (affect1 != 1) {
					new MsgBox().errorTip(FrmWithdraw, "密码错误，提交提现申请失败！");
				} else if (Integer.parseInt(txtMoney.getText()) > Integer.parseInt(LoginedUserHelper.getLoginedUserMark())) {
					new MsgBox().errorTip(FrmWithdraw, "余额不足，提交提现申请失败！");
				} else {
					int affect2 = 0;
					affect2 = new UserService().withdrawRequest(LoginedUserHelper.getLoginedUserUsername() + "", txtMoney.getText());
					if (affect2 == 1) {
						new MsgBox().infoTip(FrmWithdraw, "提交提现申请成功，请等待管理员处理！");
						FrmWithdraw.close();
					}
				}
			}
		});
		btnConfirm.addTraverseListener(new TraverseListener() {
			public void keyTraversed(TraverseEvent arg0) {
				int affect1 = 0;
				affect1 = new UserService().verifyOldPwd(txtPassword.getText(), LoginedUserHelper.getLoginedUser());
				if (affect1 != 1) {
					new MsgBox().errorTip(FrmWithdraw, "密码错误，提交提现申请失败！");
				} else if (Integer.parseInt(txtMoney.getText()) > Integer.parseInt(LoginedUserHelper.getLoginedUserMark())) {
					new MsgBox().errorTip(FrmWithdraw, "余额不足，提交提现申请失败！");
				} else {
					int affect2 = 0;
					affect2 = new UserService().withdrawRequest(LoginedUserHelper.getLoginedUserUsername() + "", txtMoney.getText());
					if (affect2 == 1) {
						new MsgBox().infoTip(FrmWithdraw, "提交提现申请成功，请等待管理员处理！");
						FrmWithdraw.close();
					}
				}
			}
		});
		btnConfirm.setBounds(106, 82, 88, 30);
		btnConfirm.setText("申请提现");

	}
}
